/**
 * 
 */
package com.geekteam.geometrychess.room;

import java.util.ArrayList;
import java.util.List;

import com.geekteam.geometrychess.room.entity.Player;
import com.geekteam.geometrychess.utils.Constants;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;

/**
 * @author MtRol
 * 
 */
public class RoomBoard {
	private int mRoomId;
	private String mName;
	private String mDescription;
	private List<Player> mPlayer;
	private List<Player> mSpector;
	private boolean isPlaying;
	
	public RoomBoard(int id, String name) {
		this.mRoomId = id;
		this.mName = name;
		this.mDescription = "description here";
		mPlayer = new ArrayList<Player>();
		mSpector = new ArrayList<Player>();
		isPlaying = false;
	}

	public void addPlayer(Player player) {
		if (player == null) {
			return;
		}

		if (!isPlaying && mPlayer.size() < Constants.MAX_PLAYERS_TO_PLAY) {
			if (mPlayer.size() == 0) {
				player.isOwner = true;
				player.isWinner = true;

			} else {
				player.isOwner = false;
				player.isWinner = false;
			}
			player.isSpector = false;
			mPlayer.add(player);
		} else {
			player.isSpector = true;
			mSpector.add(player);
		}
	}

	public void removePlayer(int sfsId) {
		if (removeSpector(sfsId)) {
			return;
		}
		if (mPlayer != null) {
			for (Player player : mPlayer) {
				if (player.mSfsId == sfsId) {
					mPlayer.remove(player);
					if (mSpector.size() > 0) {
						synchronized (RoomBoard.class) {
							if (mSpector.size() > 0) {
								mSpector.get(0).isSpector = false;
								mPlayer.add(mSpector.get(0));
								mSpector.remove(0);
							}
						}
					}
					break;
				}
			}
			if (mPlayer.size() > 0) {
				for (int i = 0; i < mPlayer.size(); i++) {
					if (i == 0) {
						mPlayer.get(i).isOwner = true;
						mPlayer.get(i).isWinner = true;
					} else {
						mPlayer.get(i).isOwner = false;
						mPlayer.get(i).isWinner = false;
					}
				}
			}
		}
	}

	public int getNumberPlayerInRoom(){
		if(mPlayer==null)
			return 0;
		return mPlayer.size();
	}
	
	public int getStartId(){
		for(Player p: mPlayer){
			if(p.isOwner){
				return p.mSfsId;
			}
		}
		return -1;
	}
	
	public boolean isOwner(int sfsId){
		for(Player p: mPlayer){
			if(p.mSfsId == sfsId)
				return p.isOwner;
		}
		
		return false;
	}
	
	public boolean isTurn(int sfsId){
		for(Player p: mPlayer){
			if(p.mSfsId == sfsId)
				return p.isTurn;
		}
		
		return false;
	}
	
	public void setTurn(int sfsId){
		for(Player p: mPlayer){
			if(p.mSfsId == sfsId){
				p.isTurn = true;
			}else{
				p.isTurn = false;
			}
		}
	}
	
	public SFSObject getInfoOfBoard() {
		SFSObject result = new SFSObject();
		result.putInt("roomId", mRoomId);
		result.putUtfString("roomName", mName);
		result.putUtfString("description", mDescription);
		result.putBool("isPlaying", isPlaying);
		result.putSFSArray("userList", getInfoOfPlayer(mPlayer));
		result.putSFSArray("spector", getInfoOfPlayer(mSpector));
		return result;
	}

	private boolean removeSpector(int sfsId) {
		if (mSpector != null) {
			for (Player player : mSpector) {
				if (player.mSfsId == sfsId) {
					mSpector.remove(player);
					return true;
				}
			}
		}
		return false;
	}

	private SFSArray getInfoOfPlayer(List<Player> players) {
		if (players == null) {
			return null;
		}
		SFSArray array = new SFSArray();
		for (Player player : players) {
			SFSObject obj = (SFSObject) player.toSFSObjectFull();
			array.addSFSObject(obj);
		}

		return array;
	}
	
	public void setPlaying(boolean play){
		isPlaying = play;
	}
	
	public boolean isPlaying(){
		return isPlaying;
	}
}
